#include<vector>
#include<algorithm>
using namespace std;

class Solution {
private:
    struct Unit {
        int difficulty;
        int profit;
        Unit(int difficulty, int profit) :difficulty(difficulty), profit(profit)
        {
        }
    };

public:
    int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
        vector<Unit> tasks;
        for (int i = 0; i < difficulty.size(); ++i)
            tasks.emplace_back(difficulty[i], profit[i]);
        sort(tasks.begin(), tasks.end(), [](const Unit& left, const Unit& right)
            {
                return left.difficulty < right.difficulty;
            });
        sort(worker.begin(), worker.end(), less<int>());
        int res = 0;
        int maxProfit = 0;
        for (int i = 0, j = 0; i < worker.size(); ++i) {
            while (j<tasks .size()&&worker[i] >= tasks[j].difficulty) {
                maxProfit = max(maxProfit, tasks[j].profit);
                ++j;
            }
            res += maxProfit;
        }
        return res;
    }
};